package com.ruoyi.ucar.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.ucar.domain.TProduct;
 import com.ruoyi.ucar.domain.po.ProductDetailQueryPo;
import com.ruoyi.ucar.domain.po.ProductMobileListPo;
import com.ruoyi.ucar.domain.po.ProductQueryListPo;
import com.ruoyi.ucar.moreq.ProductDetailQueryMoReq;
import com.ruoyi.ucar.moreq.ProductListQueryMoReq;
import com.ruoyi.ucar.moreq.ProductMobileListQueryMoReq;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;

/**
 * 产品Mapper接口
 * 
 * @author sungangbin
 * @date 2025-05-13
 */
@Mapper
public interface TProductMapper extends BaseMapper<TProduct>
{
    /**
     * 查询产品
     * 
     * @param id 产品主键
     * @return 产品
     */
    public TProduct selectTProductById(Long id);

    /**
     * 查询产品列表
     * 
     * @param tProduct 产品
     * @return 产品集合
     */
    public List<TProduct> selectTProductList(TProduct tProduct);

    /**
     * 新增产品
     * 
     * @param tProduct 产品
     * @return 结果
     */
    public int insertTProduct(TProduct tProduct);

    /**
     * 修改产品
     * 
     * @param tProduct 产品
     * @return 结果
     */
    public int updateTProduct(TProduct tProduct);

    public int tProductStoreNumberMinusone(TProduct tProduct);

    /**
     * 删除产品
     * 
     * @param id 产品主键
     * @return 结果
     */
    public int deleteTProductById(Long id);

    /**
     * 批量删除产品
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteTProductByIds(Long[] ids);


     @Select("<script>" +
             "SELECT  " +
             "   p.id as id,  " +
             "   p.product_uuid as productUuid,  " +
             "   p.product_name as productName,  " +
             "   p.product_type as productType,  " +
             "   p.file_main_uuid as fileMainUuid,  " +
             "   p.product_price as productPrice,  " +
             "   p.product_purchasing_price as productPurchasingPrice,  " +
             "   p.product_real_price as productRealPrice,  " +
             "   p.product_agency_fee as productAgencyFee,  " +
             "   p.product_return_price as productReturnPrice,  " +
             "   p.money_unit as moneyUnit,  " +
             "   p.return_price_days as returnPriceDays,  " +
             "   p.product_status as productStatus,  " +
             "   p.store_number as storeNumber,  " +
             "   p.used_number as usedNumber,  " +
             "   p.total_number as totalNumber,  " +
             "   p.remark as remark,  " +
             "   p.fix_flag as fixFlag,  " +
             "   p.sort_number as sortNumber,  " +
             "   p.second_agency_fee_ratio as secondAgencyFeeRatio,  " +
             "   d.file_url as imageUrl  " +
             "FROM  " +
             "   t_product p  " +
             "   LEFT JOIN sys_file_main f ON p.file_main_uuid = f.file_main_uuid  " +
             "   left join sys_file_data d on f.file_main_uuid=d.file_main_uuid "+
             "<where>" +
             "   <if test='queryParams.deleteFlag != null'>" +
             "       AND (p.delete_flag = #{queryParams.deleteFlag} " +
             "       AND f.delete_flag = #{queryParams.deleteFlag} " +
             "       AND d.delete_flag = #{queryParams.deleteFlag}) " +
             "   </if>" +
             "   <if test='queryParams.productStatus != null'> AND p.product_status = #{queryParams.productStatus} </if>" +
             "</where>" +
             " order by p.sort_number"+
             "</script>")
     public List<ProductMobileListPo> mobileList(@Param("queryParams") ProductMobileListQueryMoReq productMobileListQueryMoReq);


    @Select("<script>" +
            "SELECT  " +
            "   p.id as id,  " +
            "   p.product_uuid as productUuid,  " +
            "   p.product_name as productName,  " +
            "   p.product_type as productType,  " +
            "   p.file_main_uuid as fileMainUuid,  " +
            "   p.product_price as productPrice,  " +
            "   p.product_purchasing_price as productPurchasingPrice,  " +
            "   p.product_real_price as productRealPrice,  " +
            "   p.product_agency_fee as productAgencyFee,  " +
            "   p.product_return_price as productReturnPrice,  " +
            "   p.money_unit as moneyUnit,  " +
            "   p.return_price_days as returnPriceDays,  " +
            "   p.product_status as productStatus,  " +
            "   p.store_number as storeNumber,  " +
            "   p.used_number as usedNumber,  " +
            "   p.total_number as totalNumber,  " +
            "   p.remark as remark,  " +
            "   p.fix_flag as fixFlag,  " +
            "   p.sort_number as sortNumber,  " +
            "   p.second_agency_fee_ratio as secondAgencyFeeRatio,  " +
            "   d.file_url as imageUrl  " +
            "FROM  " +
            "   t_product p  " +
            "   LEFT JOIN sys_file_main f ON p.file_main_uuid = f.file_main_uuid  " +
            "   left join sys_file_data d on f.file_main_uuid=d.file_main_uuid "+
            "<where>" +
            "   <if test='queryParams.deleteFlag != null'>" +
            "       AND (p.delete_flag = #{queryParams.deleteFlag} " +
            "       AND f.delete_flag = #{queryParams.deleteFlag} " +
            "       AND d.delete_flag = #{queryParams.deleteFlag}) " +
            "   </if>" +
            "   <if test='queryParams.productStatus != null'> AND p.product_status = #{queryParams.productStatus} </if>" +
            "   <if test='queryParams.productName != null and queryParams.productName != \"\"'>AND p.product_name LIKE CONCAT('%',#{queryParams.productName},'%')</if>" +
            "  <choose> "+
            "  <when test='queryParams.storeNumberQueryType != null and queryParams.storeNumberQueryType != \"\" and  queryParams.storeNumberQueryType==\"1\" and queryParams.storeNumber!=null '>  " +
            " AND  p.store_number &lt;#{queryParams.storeNumber}  </when>  "+
            "  <when test='queryParams.storeNumberQueryType != null and queryParams.storeNumberQueryType != \"\" and  queryParams.storeNumberQueryType==\"3\" and queryParams.storeNumber!=null  '>  " +
            " AND  p.store_number &gt;#{queryParams.storeNumber}  </when>  "+
            "  <when test='queryParams.storeNumberQueryType != null and queryParams.storeNumberQueryType != \"\" and  queryParams.storeNumberQueryType==\"2\" and queryParams.storeNumber!=null  '>  " +
            " AND  p.store_number=#{queryParams.storeNumber}  </when>  "+
            " <otherwise>  </otherwise>"+
            "  </choose> "+
            "</where>" +
            " order by p.id desc "+
            "</script>")
    public IPage<ProductQueryListPo>  productListQueryPage(IPage<ProductQueryListPo> page, @Param("queryParams") ProductListQueryMoReq productListQueryMoReq);

    @Select("<script>" +
            "SELECT  " +
            "   p.id as id,  " +
            "   p.product_uuid as productUuid,  " +
            "   p.product_name as productName,  " +
            "   p.product_type as productType,  " +
            "   p.file_main_uuid as fileMainUuid,  " +
            "   p.product_price as productPrice,  " +
            "   p.product_purchasing_price as productPurchasingPrice,  " +
            "   p.product_real_price as productRealPrice,  " +
            "   p.product_agency_fee as productAgencyFee,  " +
            "   p.product_return_price as productReturnPrice,  " +
            "   p.money_unit as moneyUnit,  " +
            "   p.return_price_days as returnPriceDays,  " +
            "   p.product_status as productStatus,  " +
            "   p.store_number as storeNumber,  " +
            "   p.used_number as usedNumber,  " +
            "   p.total_number as totalNumber,  " +
            "   p.remark as remark,  " +
            "   p.fix_flag as fixFlag,  " +
            "   p.sort_number as sortNumber,  " +
            "   p.second_agency_fee_ratio as secondAgencyFeeRatio,  " +
            "   d.file_url as imageUrl  " +
            "FROM  " +
            "   t_product p  " +
            "   LEFT JOIN sys_file_main f ON p.file_main_uuid = f.file_main_uuid  " +
            "   left join sys_file_data d on f.file_main_uuid=d.file_main_uuid "+
            "<where>" +
            "   <if test='queryParams.deleteFlag != null'>" +
            "       AND (p.delete_flag = #{queryParams.deleteFlag} " +
            "       AND f.delete_flag = #{queryParams.deleteFlag} " +
            "       AND d.delete_flag = #{queryParams.deleteFlag}) " +
            "   </if>" +
            "   <if test='queryParams.id != null'> AND p.id = #{queryParams.id} </if>" +
            "   <if test='queryParams.productUuid != null and queryParams.productUuid != \"\"'> AND p.product_uuid = #{queryParams.productUuid} </if>" +
            "</where>" +
            "</script>")
    public List<ProductDetailQueryPo> productDetailQuery( @Param("queryParams")ProductDetailQueryMoReq productDetailQueryMoReq);


    public int deleteTProductByUuids(@Param("deleteFlag") Long deleteFlag,@Param("productUuids") String[] productUuids);

    public int deleteTProductByids(@Param("deleteFlag") Long deleteFlag,@Param("ids") Long[] ids);

}
